package io.gitee.welson.sensitive.strategy.impl;

import io.gitee.welson.sensitive.annotation.SensitiveStrategyComponent;
import io.gitee.welson.sensitive.strategy.SensitiveStrategy;
import io.gitee.welson.sensitive.util.SensitiveUtil;

/**
 * 姓名脱敏策略
 * 规则：
 * 1. 中文姓名：保留姓，其他用*代替
 * 2. 外文姓名：保留首字母，其他用*代替
 *
 * @author welson
 */
@SensitiveStrategyComponent
public class NameSensitiveStrategy implements SensitiveStrategy {

    private static final int MIN_LENGTH = 2;
    private static final String[] COMPOUND_SURNAMES = {
            "欧阳", "太史", "端木", "上官", "司马", "东方", "独孤", "南宫", "万俟", "闻人",
            "夏侯", "诸葛", "尉迟", "公羊", "赫连", "澹台", "皇甫", "宗政", "濮阳", "公冶",
            "太叔", "申屠", "公孙", "慕容", "仲孙", "钟离", "长孙", "宇文", "司徒", "鲜于",
            "司空", "闾丘", "子车", "亓官", "司寇", "巫马", "公西", "颛孙", "壤驷", "公良",
            "漆雕", "乐正", "宰父", "谷梁", "拓跋", "夹谷", "轩辕", "令狐", "段干", "百里",
            "呼延", "东郭", "南门", "羊舌", "微生", "公户", "公玉", "公仪", "梁丘", "公仲",
            "公上", "公门", "公山", "公坚", "左丘", "公伯", "西门", "公祖", "第五", "公乘",
            "贯丘", "公皙", "南荣", "东里", "东宫", "仲长", "子书", "子桑", "即墨", "达奚",
            "褚师", "吴铭"
    };

    @Override
    public String desensitize(String value, String... params) {
        if (SensitiveUtil.isInvalid(value, MIN_LENGTH)) {
            return value;
        }

        // 处理复姓
        for (String surname : COMPOUND_SURNAMES) {
            if (value.startsWith(surname)) {
                return surname + SensitiveUtil.createMask(value.length() - surname.length());
            }
        }

        return value.charAt(0) + SensitiveUtil.createMask(value.length() - 1);
    }
} 